/* window.c */

#include <stdlib.h>

#include "SDL.h"
#include "SDL_gfxPrimitives.h"

#include "map_area.h"
#include "graphics.h"
#include "selection.h"
#include "path.h"
#include "new_map_window.h"
#include "pony.h"
#include "maps.h"

static void proc_init_window(DLG_WIDGET *widget, int note);
static void proc_new_map_button(DLG_WIDGET *widget, int note);
static void proc_load_all_button(DLG_WIDGET *widget, int note);
static void proc_save_all_button(DLG_WIDGET *widget, int note);
static void proc_bg_image_dropdown(DLG_WIDGET *widget, int note);
static void proc_map_name_dropdown(DLG_WIDGET *widget, int note);
static void proc_seg_weight_edit(DLG_WIDGET *widget, int note);
static void proc_seg_start_level_edit(DLG_WIDGET *widget, int note);
static void proc_statue_radius_edit(DLG_WIDGET *widget, int note);

static int last_good_background;
static PATH *path;
static char footer_msg[1024] = "";
static char seg_weight[6];
static char seg_start_level[6];
static char statue_radius[6];

#define WIDGET_MAP_AREA        1
#define WIDGET_CUR_MAP         7
#define WIDGET_CUR_BG          9
#define WIDGET_SEL_WEIGHT      12
#define WIDGET_SEL_START_LEVEL 14
#define WIDGET_STATUE_RADIUS   16

DIALOG main_dialog = {
  {
    DLG_WIDGET_SCREEN(0x000044ff, proc_init_window),

    { dlg_w_arena_proc, 10, 10, 576, 576, &path, 32 },
    DLG_WIDGET_TEXT(10, 590, 590, footer_msg),

    DLG_WIDGET_BUTTON(600, 10, 100, "Load All", proc_load_all_button),
    DLG_WIDGET_BUTTON(600, 30, 100, "Save All", proc_save_all_button),
    DLG_WIDGET_BUTTON(600, 60, 100, "Create Map", proc_new_map_button),

    DLG_WIDGET_LABEL   (600, 100, "Editing map:"),
    DLG_WIDGET_DROPDOWN(600, 110, 200, map_name_items, proc_map_name_dropdown),

    DLG_WIDGET_LABEL   (600, 140, "Background image:"),
    DLG_WIDGET_DROPDOWN(600, 150, 200, bg_image_items, proc_bg_image_dropdown),

    DLG_WIDGET_LABEL(600, 300, "Segment data:"),
    DLG_WIDGET_LABEL(614, 315, "Weight:"),
    DLG_WIDGET_EDIT (678, 312, 50, seg_weight, sizeof(seg_weight), proc_seg_weight_edit),
    DLG_WIDGET_LABEL(614, 331, "Level :"),
    DLG_WIDGET_EDIT (678, 328, 50, seg_start_level, sizeof(seg_start_level), proc_seg_start_level_edit),

    DLG_WIDGET_LABEL(600, 360, "Statue radius:"),
    DLG_WIDGET_EDIT (722, 357, 50, statue_radius, sizeof(statue_radius), proc_statue_radius_edit),
  },
};

void set_footer_text(const char *str)
{
  strcpy(footer_msg, str);
  dlg_send_msg(main_dialog.widget + 2, DLGM_REDRAW, 0, 0, NULL);
}

void update_segment_info(int point, int seg)
{
  sprintf((char *) main_dialog.widget[WIDGET_SEL_WEIGHT].p,      "%d", path->points[point].segs[seg].weight);
  sprintf((char *) main_dialog.widget[WIDGET_SEL_START_LEVEL].p, "%d", path->points[point].segs[seg].start_level);
  dlg_send_msg(&main_dialog.widget[WIDGET_SEL_WEIGHT], DLGM_REDRAW, 0, 0, NULL);
  dlg_send_msg(&main_dialog.widget[WIDGET_SEL_START_LEVEL], DLGM_REDRAW, 0, 0, NULL);
}

void update_map_info(void)
{
  int point, seg;

  if (sel_get_segment(&point, &seg))
    update_segment_info(point, seg);

  sprintf((char *) main_dialog.widget[WIDGET_STATUE_RADIUS].p, "%d", path->statue.radius);
  dlg_send_msg(&main_dialog.widget[WIDGET_STATUE_RADIUS], DLGM_REDRAW, 0, 0, NULL);
}

static void proc_init_window(DLG_WIDGET *widget, int note)
{
  if (note == DLGN_INIT)
    last_good_background = 0;
}

static void proc_new_map_button(DLG_WIDGET *widget, int note)
{
  if (note == DLGN_CLICKED) {
    char new_map_name[256];
    int ok;

    ok = new_map_window(widget->dlg->screen, new_map_name, sizeof(new_map_name));
    if (ok) {
      int i;

      for (i = 0; i+1 < sizeof(map_name_items) / sizeof(map_name_items[0]) && map_name_items[i] != NULL; i++)
        ;
      if (i+1 < sizeof(map_name_items) / sizeof(map_name_items[0])) {
        map_name_items[i] = malloc(strlen(new_map_name) + 1);
        strcpy(map_name_items[i], new_map_name);
        map_name_items[i+1] = NULL;
        path = map_paths + i;
        path_clear(path);
	widget->dlg->widget[WIDGET_CUR_MAP].d1 = i;
	widget->dlg->widget[WIDGET_CUR_BG].d1 = 0;
	load_map_bg_image(bg_image_items[widget->dlg->widget[WIDGET_CUR_BG].d1]);
      }
      sel_clear();
    }
    dlg_broadcast_msg(widget->dlg, DLGM_REDRAW, 0, 0, NULL);
  }
}

static void proc_load_all_button(DLG_WIDGET *widget, int note)
{
  if (note == DLGN_CLICKED) {
    if (read_maps() != 0) {
      map_name_items[0] = "map01";
      map_name_items[1] = NULL;
      path_clear(&map_paths[0]);
      path = &map_paths[0];
    }
    widget->dlg->widget[WIDGET_CUR_MAP].d1 = 0;
    widget->dlg->widget[WIDGET_CUR_BG].d1 = map_paths[0].bg_num;
    load_map_bg_image(bg_image_items[widget->d1]);
    dlg_broadcast_msg(widget->dlg, DLGM_REDRAW, 0, 0, NULL);
  }
}

static void proc_save_all_button(DLG_WIDGET *widget, int note)
{
  if (note == DLGN_CLICKED) {
    if (save_maps() != 0)
      set_footer_text("ERROR saving maps");
    else
      set_footer_text("Saved maps");
  }
}

static void proc_bg_image_dropdown(DLG_WIDGET *widget, int note)
{
  if (note == DLGN_CHANGED) {
    if (! load_map_bg_image(bg_image_items[widget->d1]))
      widget->d1 = last_good_background;
    else
      path->bg_num = last_good_background = widget->d1;
    dlg_broadcast_msg(widget->dlg, DLGM_REDRAW, 0, 0, NULL);
  }
}

static void proc_map_name_dropdown(DLG_WIDGET *widget, int note)
{
  if (note == DLGN_CHANGED) {
    path = map_paths + widget->d1;

    if (! load_map_bg_image(bg_image_items[path->bg_num]))
      widget->dlg->widget[WIDGET_CUR_BG].d1 = last_good_background;
    else
      widget->dlg->widget[WIDGET_CUR_BG].d1 = last_good_background = path->bg_num;

    sel_clear();
    update_map_info();
    dlg_broadcast_msg(widget->dlg, DLGM_REDRAW, 0, 0, NULL);
  }
}

static void proc_seg_weight_edit(DLG_WIDGET *widget, int note)
{
  if (note == DLGN_CHANGED) {
    int point, seg;

    if (! sel_get_segment(&point, &seg)) {
      set_footer_text("No segment selected");
      return;
    }
    
    path->points[point].segs[seg].weight = atoi((char *) widget->p);
  }
}

static void proc_seg_start_level_edit(DLG_WIDGET *widget, int note)
{
  if (note == DLGN_CHANGED) {
    int point, seg;

    if (! sel_get_segment(&point, &seg)) {
      set_footer_text("No segment selected");
      return;
    }
    
    path->points[point].segs[seg].start_level = atoi((char *) widget->p);
  }
}

static void proc_statue_radius_edit(DLG_WIDGET *widget, int note)
{
  if (note == DLGN_CHANGED) {
    path->statue.radius = atoi((char *) widget->p);
    dlg_send_msg(&main_dialog.widget[WIDGET_MAP_AREA], DLGM_REDRAW, 0, 0, NULL);
  }
}

